<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>主页</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/plugins/build/css/app.css" media="all">
</head>
<body>
<div class="container">
	<form class="layui-form custom_search_form" action="">
		<div class="layui-form-item">
		    <label class="layui-form-label">昵称</label>
		    <div class="layui-input-block">
		      <input type="text" name="nickname" placeholder="请输入标题" autocomplete="off" class="layui-input">
		    </div>
	  	</div>
	    <div class="layui-form-item">
		    <label class="layui-form-label">登录账号</label>
		    <div class="layui-input-block">
		      <input type="text" name="loginAccount" placeholder="请输入标题" autocomplete="off" class="layui-input">
		    </div>
	  	</div>
	    <div class="custom_btn_search">
	      	<button class="layui-btn" lay-submit="" lay-filter="search">搜索</button>
	      	<button class="layui-btn layui-btn-primary layui-btn-sm" onclick="add();return false;">
	            <i class="layui-icon">&#xe654;</i>
	        </button>
	  	</div>
	</form>
	<table id="table" lay-filter="table"></table>
</div>
<script src="/plugins/layui/layui.js"></script>
<script src="/plugins/common.js"></script>
<script>
	//配置项：列表地址、删除地址、编辑页面、详情页面
    var listUrl = SERVER_URL+"/sysUser/list";
    var delUrl = SERVER_URL+"/sysUser/del";
    var editUrl = "edit.html";
    var roleUrl = "role.html";

    //配置项：列表高度
    var tableHeight = "full-140";

    //配置项：主键名称
    var ID = "userId";

    layui.use(['jquery', 'table', 'form','layer'], function() {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        layer.config({
            anim: 5
            ,offset: '50px'
            ,maxmin: true
            ,isOutAnim: false,
            shade: [0.5, '#eee']
		})

		//配置项：列表项
        table.render({
            elem: '#table'
            ,height: tableHeight
            ,url: listUrl
            ,page: true
            ,cols: [[
                {field: 'nickname', title: '用户名称'}
                ,{field: 'loginAccount', title: '登录账号'}
                ,{fixed: 'right', title: '操作', width:150, toolbar: '#toolBar'}
            ]]
        });

        //列表行末工具栏
	  	table.on('tool(table)', function(obj){
		  	if(obj.event === 'role'){
                layer.open({
                    area: ['800px', '500px']
					,type: 2
                    ,title: '角色'
                    ,content: roleUrl+"?id="+obj.data[ID]
                });
		  	} else if(obj.event === 'edit'){
				layer.open({
                    area: ['800px', '500px']
					,type: 2
                    ,title: '编辑'
				  	,content: editUrl+"?id="+obj.data[ID]
				});
		  	} else if(obj.event === 'del'){
                layer.confirm('确定删除该用户？', {
                    btn: ['确定','取消']
                }, function(){
                    layer.load(2, {time: 5*1000});
                    $.ajax({
                        type: 'POST',
                        url: delUrl,
                        data: {id: obj.data[ID]},
                        success: function (result) {
                            layer.closeAll();
                            if(result.code == 20000){
                                layer.msg('操作成功', {icon: 1,time: 1000}, function(){
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(result.msg, {time: 1500});
                            }
                        }
                    });
                }, function(){
                    layer.closeAll();
                });
		  	}
		});

	  	//条件搜索
	  	form.on('submit(search)', function(data){
		    table.reload('table', {
			  	url: listUrl
			  	,where: data.field 
			  	,page:{ curr: 1 }
		    	,height: tableHeight
			});
		    return false;
	  	});
    });

    //添加页面
    function add(){
        layer.open({
            area: ['800px', '500px']
			,type: 2
            ,title: '添加'
            ,content: editUrl
        });
	}

	//刷新列表
    function reload() {
        layer.closeAll();
        layer.msg('操作成功', {icon: 1,time: 1500}, function(){
            window.location.reload();
        })
    }
</script>
<script type="text/html" id="toolBar">
	<div class="layui-btn-group custom_btn_group">
		{{#  if(d.userId != '-1'){ }}
	  	<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="role">
	    	<i class="layui-icon">&#xe66f;</i>
	  	</button>
		{{#  } }}
  		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit">
	    	<i class="layui-icon">&#xe642;</i>
	  	</button>
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="del">
			<i class="layui-icon">&#xe640;</i>
		</button>
	</div>
</script>
</body>
</html>